package admin

import (
	"gitee.com/sansaniot/ssiot-core/facade/httpserver"
	"gitee.com/sansaniot/ssiot-core/sdk/pkg"

	"ssadmin/cmd/system"
	"ssadmin/common/middleware"
	_var "ssadmin/common/vars"
	"ssadmin/internal/admin/router"
)

func Start() error {
	// 如有需要，向内核注册自己需要的中间件
	// 限流
	httpserver.RegisterMiddlewareFunction(middleware.Sentinel())
	// 自动增加requestId
	httpserver.RegisterMiddlewareFunction(middleware.RequestId(pkg.TrafficKey))
	// 自定义错误处理
	httpserver.RegisterMiddlewareFunction(middleware.CustomError)
	// NoCache is a middleware function that appends headers
	httpserver.RegisterMiddlewareFunction(middleware.NoCache)
	// 跨域处理
	httpserver.RegisterMiddlewareFunction(middleware.Options)
	// Secure is a middleware function that appends security
	httpserver.RegisterMiddlewareFunction(middleware.Secure)

	// 必须，初始化引擎
	httpserver.InitEngine(_var.ConfigFile)

	// 启动系统业务
	system.InitSystemBiz()

	// 可选，初始化JWT验证方法组
	jwtMiddleware, _ := middleware.AuthInit()
	// 必须，向内核注册自己的业务模块路由，如不使用中间件，则使用InitRouters方法
	httpserver.InitAuthRouters(router.AuthRouters, jwtMiddleware)
	// 启动HTTP服务器
	_ = httpserver.Run()

	return nil
}
